package com.web;

import com.domain.User;
import com.service.userService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
 * 客户的登录
 * 接收 LoginUser.jsp 页面的 phone、 password数据
 * 提交到 userService 的 loginUser 函数，查询用户数据
 * 重定向到 /ShowUserSelf
 */
@WebServlet("/LoginUser")//客户进行登录
public class LoginUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.解决乱码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        //2.获取数据
        String phone = request.getParameter("phone");
        String password = request.getParameter("password");
        System.out.println(phone +"客户在登录");
        //3.比较
        User user = new userService().loginUser(phone, password);

        //4.登录信息展示
        PrintWriter responseWriter = response.getWriter();
        if( user != null){
            //登陆成功
            //重定向到主页面
            response.sendRedirect(request.getContextPath()+"/ShowUserSelf?userId="+user.getUserId());//重定向到展示客户的所有活动
            System.out.println(phone+"客户登录成功");
        }else{
            //登录失败"
            responseWriter.write("该用户名密码不正确<a href=\"LoginUser.jsp\">重新登录</a>");
            System.out.println(phone+"客户登录失败");
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
